// pages/personal/personal.js
let startY = 0;
import { getStorageSync } from '../../utils/util.js'
import { REQUEST_USER_RECORD } from '../../api/index.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    coverTransform: 'translateY(0)',
    coverTransition: '',
    userInfo: {},
    defaultAvatar: '/static/images/personal/missing-face.png',
    recentPlayList: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const userInfo = getStorageSync('userInfo');
    if (!userInfo.userId) return;
    this.setData({ userInfo });
    this.getUserPlayListData(userInfo.userId);
  },
  async getUserPlayListData(userId) {
    const { allData, code } = await REQUEST_USER_RECORD({uid: userId, type: 0 });
    if (code !== 200) return;
    this.setData({ recentPlayList: allData.splice(0, 10).map((item, index) => ({...item, id: index})) });
  },
  handerTouchStart(evt) {
    this.setData({
      coverTransition: ''
    });
    startY = evt.touches[0].clientY;
  },
  handerTouchMove(evt) {
    const moveY = evt.touches[0].clientY;
    let moveDistance = moveY - startY;
    if (moveDistance <= 0 ) return;
    if (moveDistance >= 80 ) moveDistance = 80;
    this.setData({
      coverTransform: `translateY(${moveDistance}rpx)`
    });

  },
  handerTouchEnd(evt) {
    this.setData({
      coverTransform: `translateY(0rpx)`,
      coverTransition: 'transform 1s linear'
    });
  },
  toLogin() {
    wx.navigateTo({ url: '/pages/login/login' });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})